﻿package at.geoathome.utils.font
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.Font;

	/**
	 * ...
	 * @author Georg Kamptner & Thomas Klana
	 */
	public class EmbedFontLoader extends EventDispatcher
	{
		public static const FONT_LOADED:String = "FONT_LOADED";
		
		//
		// VARIABLES:
		private static var _instance:EmbedFontLoader = null;
		
		private var _source_folder_url:String = null;
		private var _font_list:Array;
		// - - - - - - - - - - - - - - - - - - - 
		//
		
		//
		// SINGLETON:
		public function EmbedFontLoader() 
		{
			_font_list = new Array();
		}
		
		public static function getInstance( ):EmbedFontLoader
		{
			if(EmbedFontLoader._instance == null) {
				EmbedFontLoader._instance = new EmbedFontLoader();
			}
			return EmbedFontLoader._instance;
		}
		// - - - - - - - - - - - - - - - - - - - 
		//
		
		//
		// FUNCTIONS & METHODS:
		private function load(font_name:String):void
		{	
			// break condition
			if (fontExists(font_name)) return;
			
			var f:EmbedFont = new EmbedFont(font_name, _source_folder_url);
			f.addEventListener(Event.COMPLETE, onFontLoadComplete);
			f.load(font_name, _source_folder_url);
			
			_font_list.push(f);
		}
		
		private function fontExists(font_name:String):Boolean
		{
			for (var i:String in Font.enumerateFonts())
			{
				if (Font.enumerateFonts()[i]().fontName == font_name)
					return true;
			}
			
			return false;
		}
		
		/*
		public function getFont( font_name:String ):Class {			
			//trace(font_name + ": " +  this.loadedFonts[ font_name ]);
			return this._loaded_fonts[ font_name ];
        }
		*/
		
		public function onFontLoadComplete(e:EmbedFontEvent):void
		{	
			dispatchEvent( new EmbedFontEvent(e.fontName, EmbedFontLoader.FONT_LOADED) );					   
		}

		public function setSourceFolderURL(url:String):void
		{
			_source_folder_url = url;
		}
		
		public function getSourceFolderURL():String
		{
			return _source_folder_url;
		}
		
		public function getFont(font_name:String):Font
		{
			for (var i:String in Font.enumerateFonts())
			{
				if (Font.enumerateFonts()[i].fontName == font_name)
					return Font.enumerateFonts()[i] as Font;
			}
			
			return null;
		}
		
		// -- PUBLIC STATIC INTERFACE ---------------------------------------------------------------
		public static function load(font_name:String) :void
		{
			EmbedFontLoader.getInstance().load(font_name);
		}
		
		public static function setSourceFolderURL(url:String):void
		{
			EmbedFontLoader.getInstance().setSourceFolderURL(url);
		}
		
		public static function getSourceFolderURL():String
		{
			return EmbedFontLoader.getInstance().getSourceFolderURL();
		}
		
		public static function getFont(font_name:String):Font
		{
			return EmbedFontLoader.getInstance().getFont(font_name);
		}

	}
}
